#include<graphics.h>

#include<conio.h>

#include<stdio.h>

#include<time.h>

#include<stdlib.h>

#define Width 500

#define Height 432

int xLines=10;
int f[20]={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19};
int yLines=10;
int gridWidth=Width/10;
int gridHeight=Height/8;
int sc=0;
void loadAllImage();//加载图片
void drawGrid();//画格子
void drawRedGrid();//画拼图区格子
void mouseControl();//鼠标控制
void Match();

IMAGE a[20];//待选区图片
IMAGE M[20];//待匹配图片
IMAGE Origin;
IMAGE Tips;

int main()
{   srand((unsigned)time(NULL));
	int q=0;
	int h=0;
	for(int i=0;i<20;i++)//0到20随机排序
	{
		
		q=rand()%10;
		h=f[i];
		f[i]=f[q];
		f[q]=h;
	}
	
	initgraph(Width,Height);
	setbkcolor(WHITE);
	cleardevice();
	loadAllImage();
	drawGrid();
	loadimage(&Tips,_T("./images/b.jpg"));//加载提示
	putimage(205,0,&Tips);
	drawRedGrid();
	mouseControl();
	Match();
	//_getch();

	closegraph();

	printf("you win");
	return 0;
}

void drawGrid()
{
for(int i=0;i<xLines;i++)
	{
		setlinecolor(RGB(0,0,0));
		line(i*gridWidth,0,i*gridWidth,432);
	}
	for(int i=0;i<yLines;i++)
	{
		setlinecolor(RGB(0,0,0));
		line(0,i*gridHeight,500,i*gridHeight);
	}
}

void loadAllImage()
{
	int j=-11;
    loadimage(&a[0],_T("./images/IMG_01.gif"));
	loadimage(&a[1],_T("./images/IMG_02.gif"));
	loadimage(&a[2],_T("./images/IMG_03.gif"));
	loadimage(&a[3],_T("./images/IMG_04.gif"));
	loadimage(&a[4],_T("./images/IMG_05.gif"));
	loadimage(&a[5],_T("./images/IMG_06.gif"));
	loadimage(&a[6],_T("./images/IMG_07.gif"));
	loadimage(&a[7],_T("./images/IMG_08.gif"));
	loadimage(&a[8],_T("./images/IMG_09.gif"));
	loadimage(&a[9],_T("./images/IMG_10.gif"));
	loadimage(&a[10],_T("./images/IMG_11.gif"));
	loadimage(&a[11],_T("./images/IMG_12.gif"));
	loadimage(&a[12],_T("./images/IMG_13.gif"));
	loadimage(&a[13],_T("./images/IMG_14.gif"));
	loadimage(&a[14],_T("./images/IMG_15.gif"));
	loadimage(&a[15],_T("./images/IMG_16.gif"));
	loadimage(&a[16],_T("./images/IMG_17.gif"));
	loadimage(&a[17],_T("./images/IMG_18.gif"));
	loadimage(&a[18],_T("./images/IMG_19.gif"));
	loadimage(&a[19],_T("./images/IMG_20.gif"));



	loadimage(&Origin,_T("./images/a.jpg"));//加载原图
	putimage(300,0,&Origin);

   
    for(int i=0;i<20;i++)
	{
		int c;
		c=f[i];
		j++;
		if(i<10){
			putimage(50*i,324,&a[c]);}
		else

		{   
			putimage(50*j,378,&a[c]);
		}
	}
	
	
	
}

void drawRedGrid()
{
	for(int i=0;i<5;i++)
	{
		setlinecolor(RED);
		line(i*gridWidth,0,i*gridWidth,270);
	}
	for(int i=0;i<6;i++)
	{
		setlinecolor(RED);
		line(0,i*gridHeight,200,i*gridHeight);
	}
}

void mouseControl()
{
	getimage(&M[0],0,0,54,50);
	int x,y,q,r;
	while(1)
	{
		
		if(&M[0]==&a[0])
       {break;}
		MOUSEMSG m;
		if(MouseHit())
		{
			m=GetMouseMsg();
			if(m.mkLButton)
			{
				x=m.x;
				y=m.y;
				q=m.x;
				r=m.y;
				for(int i=0;i<10;i++)
				{
					if((m.y>324)&&(m.y<378)&&(m.x>gridWidth*i)&&(m.x<gridWidth*(i+1)))
					{
						x=i*50;
						y=324;
						getimage(&a[f[i]],x,y,50,54);
						sc=f[i];
					}
					else
					if((m.y>378)&&(m.y<432)&&(m.x>gridWidth*i)&&(m.x<gridWidth*(i+1)))
					{
						x=i*50;
						y=378;
						getimage(&a[f[i+10]],x,y,50,54);
						sc=f[i+10];
					}
				}
				for(int i=0;i<4;i++)
				{
					for(int j=0;j<5;j++)
					{
						if((m.y>=j*gridHeight)&&(m.y<=(j+1)*gridHeight)&&(m.x>gridWidth*i)&&(m.x<gridWidth*(i+1)))
						{
							q=i*50;
				            r=j*54;
				            putimage(q,r,&a[sc]);
						}
					}
				}
			}
		}
		
		
	
	}

}

void Match()
{
	
	/*if(m[0]==a[0])

    {

		

		printf("you win");

		system("pause");

	}*/
	/*for(int i=0;i<4*gridWidth;i+=gridWidth)

	{

		for(int j=0;j<5*gridHeight;j+=gridHeight)

		{

			if()

			{

			}

		}

	}*/
}